angular.module('start.guide.ctrl', [])

  .controller('GuideCtrl', function($scope, $$GuideService, $ionicLoading, $rootScope, $stateParams, $timeout) {
    console.info("GuideCtrl");
    // console.info("guideCtrl" + angular.element("#abc").html());
    $scope.$on("$ionicView.loaded", function() {
      console.info("GuideCtrl--$ionicView.loaded ");
    });



    // 我的分类 数据
    $scope.guideData = [];

    $scope.pageData = {
      isloading: true,
      pageSize: 4,
      pageNum: 1,
      nomoreData: false
    };

    // 我的分类 当前分类 1.热门 2.关注 3.附近
    $scope.mycateNo = 1;
    $scope.serviceKinds = $stateParams.serviceKinds || null;

    // 改变展示的分类
    $scope.changemycateNo = function(id) {
      if (id == 2) {
        // 关注
        if ($rootScope.isLogined() == false) {
          $ionicLoading.show({
            template: "请登录",
            noBackdrop: true,
            duration: 1500
          });
          return;
        }
      }
      if ($scope.mycateNo !== id) {
        $scope.mycateNo = id;
        $scope.guideData = [];
        $scope.pageData = {
          isloading: true,
          pageSize: 4,
          pageNum: 1,
          nomoreData: false
        };
        $scope.getGuidePageList();
      }
    };

    // 改变服务分类
    $scope.changeServiceKinds = function(id) {
      if ($scope.serviceKinds !== id) {
        $scope.serviceKinds = id;
        $scope.guideData = [];
        $scope.pageData = {
          isloading: true,
          pageSize: 4,
          pageNum: 1,
          nomoreData: false
        };
        $scope.getGuidePageList();
      }
    };

    $scope.mycate = function(mycateid) {
      $scope.mycateNo = mycateid;
    };

    // 下拉刷新
    $scope.doRefresh = function() {
      $scope.guideData = [];
      $scope.pageData = {
        isloading: true,
        pageSize: 4,
        pageNum: 1,
        nomoreData: false
      };
      $scope.getGuidePageList();
    };

    // 向下翻页
    $scope.loadMore = function() {
      console.log("loadMore");
      if ($scope.pageData.nomoreData === false && !$scope.pageData.isloading) {
        $scope.getGuidePageList();
      } else {
        $scope.$broadcast('scroll.infiniteScrollComplete');
      }
    };

    // 获取向导列表数据
    $scope.getGuidePageList = function() {
      $scope.pageData.isloading = true;

      if ($scope.mycateNo == 1 || $scope.mycateNo == null) {
        // 获取向导列表数据
        $$GuideService.getGuideList($rootScope.config.city.name, $scope.pageData.pageNum, $scope.pageData.pageSize, $scope.serviceKinds)
          .then(function(res) {
            if (res.data == undefined) {
              res.data = { list: [] };
            }
            if (res.errCode === 203) {
              $scope.guideData = $scope.guideData.concat(res.data.list);
              $scope.$broadcast('scroll.infiniteScrollComplete');
              $scope.$broadcast('scroll.refreshComplete');
              // 如果没有更多数据 数据到底了
              $scope.pageData.pageNum++;
              if (res.data.list.length < $scope.pageData.pageSize) {
                $scope.pageData.nomoreData = true;
              } else {
                $scope.pageData.nomoreData = false;
              }
            } else {
              $ionicLoading.show({
                template: res.msg,
                noBackdrop: true,
                duration: 1500
              });
            }
            $scope.pageData.isloading = false;
          }, function() {
            console.info();
            $scope.pageData.isloading = false;
          });

      } else if ($scope.mycateNo == 2) {
        // 获取关注向导列表数据
        $$GuideService.getAttentionList($rootScope.config.city.name, $scope.pageData.pageNum, $scope.pageData.pageSize, $scope.serviceKinds)
          .then(function(res) {
            if (res.errCode === 203) {
              if (res.data == undefined) {
                res.data = [];
              }
              $scope.guideData = $scope.guideData.concat(res.data);
              $scope.$broadcast('scroll.infiniteScrollComplete');
              $scope.$broadcast('scroll.refreshComplete');
              // 如果没有更多数据 数据到底了
              $scope.pageData.pageNum++;
              if (res.data.length < $scope.pageData.pageSize) {
                $scope.pageData.nomoreData = true;
              } else {
                $scope.pageData.nomoreData = false;
              }
            } else {
              $ionicLoading.show({
                template: res.msg,
                noBackdrop: true,
                duration: 1500
              });
            }
            $scope.pageData.isloading = false;
          }, function() {
            console.info();
            $scope.pageData.isloading = false;
          });

      } else if ($scope.mycateNo == 3) {
        // 获取向导列表数据 附近
        $$GuideService.getArroundList($rootScope.config.city.name, $scope.pageData.pageNum, $scope.pageData.pageSize, $scope.serviceKinds)
          .then(function(res) {
            if (res.errCode === 203) {
              if (res.data == undefined) {
                res.data = [];
              }
              $scope.guideData = $scope.guideData.concat(res.data);
              $scope.$broadcast('scroll.infiniteScrollComplete');
              $scope.$broadcast('scroll.refreshComplete');
              // 如果没有更多数据 数据到底了
              $scope.pageData.pageNum++;
              if (res.data.length < $scope.pageData.pageSize) {
                $scope.pageData.nomoreData = true;
              } else {
                $scope.pageData.nomoreData = false;
              }
            } else {
              $ionicLoading.show({
                template: res.msg,
                noBackdrop: true,
                duration: 1500
              });
            }
            $scope.pageData.isloading = false;
          }, function() {
            console.info();
            $scope.pageData.isloading = false;
          });

      }

    };
    $scope.getGuidePageList();



  });
